﻿#region Copyright and License Notice
// Copyright (C)2010-2016 - Rob Levine and other contributors
// http://configgen.codeplex.com
// 
// This file is part of ConfigGen.
// 
// ConfigGen is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// ConfigGen is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License and 
// the GNU Lesser General Public License along with ConfigGen.  
// If not, see <http://www.gnu.org/licenses/>
#endregion

using System;
using System.Data;
using System.IO;
using System.Reflection;
using ConfigGen.Core.SettingsLoader;
using ConfigGen.Utilities.Extensions.System.Reflection;
using NUnit.Framework;

namespace ConfigGen.Core.Tests.SettingsLoader
{
    [TestFixture]
    public class ExcelFileLoaderTests
    {
        [Test]
        public void GetSettingsDataset001_BadArgs()
        {
            var itemUnderTest = new ExcelFileLoader();
            Assert.Throws<ArgumentNullException>(() => itemUnderTest.GetSettingsDataSet(null), "Null arg should have thrown ArgumentNullException");
            Assert.Throws<ArgumentException>(() => itemUnderTest.GetSettingsDataSet(string.Empty), "Empty arg should have thrown ArgumentException");
        }

        [Test]
        public void GetSettingsDataset002_FileNotFound()
        {
            var itemUnderTest = new ExcelFileLoader();
            Assert.Throws<FileNotFoundException>(() => itemUnderTest.GetSettingsDataSet("noSuchFile"));
        }

        [Test]
        public void GetSettingsDataset003_XlsFile()
        {
            using (var testFile = Assembly.GetExecutingAssembly().GetEmbeddedResourceFile("SettingsLoader.SampleFiles.App.Config.Settings.xls", "App.Config.Settings.xls"))
            {
                var itemUnderTest = new ExcelFileLoader();
                DataSet loadedDataset = itemUnderTest.GetSettingsDataSet(testFile.FullName);
                Assert.IsNotNull(loadedDataset, "loaded dataset should not be null");
                Assert.AreEqual(1, loadedDataset.Tables.Count, "incorrect number of datatables in loaded spreadsheet");
                var sheet1 = loadedDataset.Tables["Sheet1"];
                var headerRow = sheet1.Rows[0];
                Assert.AreEqual("MachineName", headerRow.ItemArray[0], "Incorrect heading for first column in sheet1");
            }
        }

        [Test]
        public void GetSettingsDataset004_XlsxFile()
        {
            using (var testFile = Assembly.GetExecutingAssembly().GetEmbeddedResourceFile("SettingsLoader.SampleFiles.App.Config.Settings.xlsx", "App.Config.Settings.xlsx"))
            {
                var itemUnderTest = new ExcelFileLoader();
                DataSet loadedDataset = itemUnderTest.GetSettingsDataSet(testFile.FullName);
                Assert.IsNotNull(loadedDataset, "loaded dataset should not be null");
                Assert.AreEqual(1, loadedDataset.Tables.Count, "incorrect number of datatables in loaded spreadsheet");
                var sheet1 = loadedDataset.Tables["Sheet1"];
                var headerRow = sheet1.Rows[0];
                Assert.AreEqual("MachineName", headerRow.ItemArray[0], "Incorrect heading for first column in sheet1");
            }
        }

        [Test]
        public void GetSettingsDataset005_UnrecognisedFileTypeThrowsException()
        {
            using (var testFile = Assembly.GetExecutingAssembly().GetEmbeddedResourceFile("SettingsLoader.SampleFiles.App.Config.Settings.xlsx", "App.Config.Settings.badExtension"))
            {
                var itemUnderTest = new ExcelFileLoader();
                Assert.Throws<NotSupportedException>(() => itemUnderTest.GetSettingsDataSet(testFile.FullName));
            }
        }
    }
}
